﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SendSingleSMS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadLogin();
        }
    }


    private void loadLogin()
    {
        if (hfLoginID.Value == "")
        {
            hfLoginID.Value = getLogin().ToString();
        }
    }

    private string getLoginID()
    {
        return hfLoginID.Value.Substring(6, hfLoginID.Value.Length - 6).Substring(0, hfLoginID.Value.Length - 12);


    }
    private string getLogin()
    {
        string loginID = "";
        try
        {
            if (Session["OnlineSMS_LoginID"] != null)
            {
                loginID = Session["OnlineSMS_LoginID"].ToString();
            }
            else if (hfLoginID.Value != "")
            {
                loginID = hfLoginID.Value;
            }
            else
            {
                Session["PreviousPage"] = HttpContext.Current.Request.Url.AbsoluteUri; Response.Redirect("Default.aspx");
            }

        }
        catch (Exception ex)
        { }

        return loginID;
    }
   
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (txtMasking.Text.Trim() == "")
            return;

        if (txtMobileNo.Text.Trim() == "")
            return;

        if (txtSMS.Text.Trim() == "")
            return;

        string mobileNo=txtMobileNo.Text;
        if (!mobileNo.Contains(","))
            mobileNo += ",";
        int totalSend = 0;
        int totalNotSend = 0;
        int noOfSMS = SendSMS.countSMS(txtSMS.Text.Trim());
        string NotSendNo = "";
        try
        {
            
            
            foreach (string  no in mobileNo.Split(','))
            {
                if (no.Trim() != "")
                {
                    if (SendSMS.sendSMS(getLoginID(), txtMasking.Text.Trim(), no, txtSMS.Text))
                    {
                        totalSend++;
                    }
                    else
                    {
                        NotSendNo += (NotSendNo == "" ? "" : ",") + no;
                        totalNotSend++;
                    }
                }
            }

            string sql = @"

update Login set RemainingSMS-=" + (noOfSMS * totalContactTosend()).ToString() + " where LoginID=" + getLoginID() + @"
                    
INSERT INTO [OnlineSMS].[dbo].[SMS_Job]
           ([SMS]
           ,[LoginID]
           ,[StatusID]
           ,[JobSubmissionDate]
           ,[JobStartedDate]
           ,[LastUpdateDate]
           ,[Size]
            ,[TotalContactTosent]
           ,[LetterCount]
           ,[Masking]
           ,[SendToGroupIDs]
           ,[SendToCount]
           ,[NotSendToCount]
           ,[NotSendTo])
     VALUES
           ('" + txtSMS.Text.Replace("'", "''") + @"'--<SMS, nvarchar(max),>
           ," + getLoginID() + @"--<LoginID, bigint,>
           ,1--<StatusID, int,>
           ,GETDATE()--<JobSubmissionDate, datetime,>
           ,GETDATE()--<JobStartedDate, datetime,>
           ,GETDATE()--<LastUpdateDate, datetime,>
           ," + noOfSMS.ToString() + @"--<Size, int,>
           ," + totalContactTosend() + @"--<TotalContactTosent, bigint,>
            ," + txtSMS.Text.Trim().Length.ToString() + @"--<LetterCount, int,>
            ,'" + txtMasking.Text + @"'--<Masking, nvarchar(11),>
           ,'" + txtMobileNo.Text + @"'--<SendToGroupIDs, nvarchar(max),>
           ," + totalSend + @"--<SendToCount, int,>
           ," + totalNotSend + @"--<NotSendToCount, int,>
           ,'" + NotSendNo + @"'--<NotSendTo, nvarchar(max),>
           );
";
            DatabaseManager.ExecSQL(sql);
            
            
        }
        catch (Exception ex)
        {
            lblReport.Text = "Error Occured !!! <br/>";
        }
            lblReport.Text = "Total SMS sent :" + totalSend + " X " + noOfSMS + " = " + (totalSend*noOfSMS).ToString();
            if(totalNotSend!=0)
                lblReport.Text += "<br/><span style='color:red;'>Total Not sending SMS:" + totalSend + " X " + noOfSMS + " = " + (totalSend * noOfSMS).ToString()+"</span>";

    }

    private bool balanceChecking()
    {

        if (SendSMS.remainingSMS(getLoginID()) >= totalContactTosend()) return true;
        else
            return false;
    }
    private int totalContactTosend()
    {
        int smsNeedTosend = 0;
        foreach (string no in (txtMobileNo.Text+",").Split(','))
        {
            if (no.Trim() != "")
            {
                smsNeedTosend++;
            }
        }
        return smsNeedTosend;
    }
}